package jp.sourceforge.akjrcp.akj_betas;

import jp.sourceforge.akjrcp.audiotools.AudioToolsPerspective;
import jp.sourceforge.akjrcp.dropsomething.DropsomethingPerspective;
import jp.sourceforge.akjrcp.flvtools.explorer.ExplorerPerspective;
import jp.sourceforge.akjrcp.psp.PSPPerspective;
import jp.sourceforge.akjrcp.tools.ToolsPerspective;
import jp.sourceforge.akjrcp.videotools.VideoToolsPerspective;

import org.eclipse.core.runtime.IExtension;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.registry.ActionSetRegistry;
import org.eclipse.ui.internal.registry.IActionSetDescriptor;
import org.eclipse.ui.internal.wizards.ExportWizardRegistry;
import org.eclipse.ui.wizards.IWizardDescriptor;
import org.eclipse.ui.wizards.IWizardRegistry;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
	//default
	private static final String PERSPECTIVE_ID=DropsomethingPerspective.ID;
	
	//private static final String PERSPECTIVE_ID=AudioToolsPerspective.ID;
	
	//private static final String PERSPECTIVE_ID = PSPPerspective.ID;
	
	//private static final String PERSPECTIVE_ID = ExplorerPerspective.ID;
	
	//private static final String PERSPECTIVE_ID = SubtitleEditPerspective.ID;
	//private static final String PERSPECTIVE_ID = VideoToolsPerspective.ID;
	//private static final String PERSPECTIVE_ID=ToolsPerspective.ID;
	//private static final String PERSPECTIVE_ID=YoutubeDLPerspective.ID;
		
	public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		
		 
		return new ApplicationWorkbenchWindowAdvisor(configurer);
	}

	public String getInitialWindowPerspectiveId() {
		  
		 
		return PERSPECTIVE_ID;
	}
	
	  public void preStartup() {
		 //remove preferences
		  WorkbenchPlugin.getDefault().getPreferenceManager().remove("org.eclipse.ui.preferencePages.Workbench");
		  WorkbenchPlugin.getDefault().getPreferenceManager().remove("org.eclipse.update.internal.ui.preferences.MainPreferencePage");
		  
		
		 
		 //didn't work. 
		  /*
		 IPerspectiveRegistry reg=WorkbenchPlugin.getDefault().getPerspectiveRegistry();
		 IPerspectiveDescriptor id=reg.findPerspectiveWithId("org.eclipse.ui.resourcePerspective");
		 if(id!=null){
		 WorkbenchPlugin.getDefault().getPerspectiveRegistry().deletePerspective(id);
		 }
		 */
		 //delete actions.org.eclipse.ui.internal.wizards.ExportWizardRegistry

		  
		 
		  //WorkbenchPlugin.getDefault().get
		  //exports.
		  //exports.findWizard("").get
		 ActionSetRegistry areg = WorkbenchPlugin.getDefault().getActionSetRegistry();
	    	IActionSetDescriptor[] actionSets = areg.getActionSets();
	    	/*
	    	for (int i = 0; i < actionSets.length; i++) {
				log.info(actionSets[i].getId());
			}
			*/
	    	
	    	String ignores[]={"org.eclipse.ui.WorkingSetActionSet","org.eclipse.ui.NavigateActionSet","org.eclipse.ui.actionSet.keyBindings","org.eclipse.update.ui.softwareUpdates"};
		  	
	    	
	    	for (int i = 0; i <actionSets.length; i++)
	    	
	    	{
	    		for(int j=0;j<ignores.length;j++){
	    	    if (!actionSets[i].getId().equals(ignores[j]))
	    	
	    	        continue;
	    	
	    	    IExtension ext = actionSets[i].getConfigurationElement()
	    	
	    	        .getDeclaringExtension();
	    	
	    	    areg.removeExtension(ext, new Object[] { actionSets[i] });
	    		}
	    	
	    	}
		  
		
		  /*
		   * for remove perspective
		  
		  IPerspectiveDescriptor des[]=list.getPerspectives();
		  for (int i = 0; i < des.length; i++) {
			log.info(des[i].getLabel()+","+des[i].getId());
		  }
		  */
		  
		  
		  /*
		   * for show plugins
		   * 
		   * 
		  List list=WorkbenchPlugin.getDefault().getPreferenceManager().getElements(WorkbenchPlugin.getDefault().getPreferenceManager().PRE_ORDER);
		  
		  Iterator iter=list.iterator();
		  while(iter.hasNext()){
			  WorkbenchPreferenceNode obj=(WorkbenchPreferenceNode)iter.next();
			  log.info(obj.getLabelText()+","+obj.getId());
		  }
		  */
	    }

	  
	    public void postStartup() {
	    	
	    	if(PERSPECTIVE_ID.equals(DropsomethingPerspective.ID)){
	    		
	    	
	    	PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().setPerspective(
					PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(VideoToolsPerspective.ID)
					);
					
	    	
				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().setPerspective(
						PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(DropsomethingPerspective.ID)
						);
	    	//log.infochanged:"+PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(ExplorerPerspective.ID));
			
	    	PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().setPerspective(
						PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(ExplorerPerspective.ID));
	    	}
				
				
			
	    }


}
